<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="入库编号" prop="warehouseInNo">
        <el-input v-model="queryParams.warehouseInNo" placeholder="请输入入库编号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="来源单号" prop="sourceNo">
        <el-input v-model="queryParams.sourceNo" placeholder="请输入来源单号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="入库类型" prop="goodsType">
        <el-select v-model="queryParams.goodsType" placeholder="请选择入库类型" clearable style="width: 192px;">
          <el-option v-for="dict in warehouse_in_store_type" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="入库状态" prop="warehouseInStatus">
        <el-select v-model="queryParams.warehouseInStatus" placeholder="请选择入库状态" clearable style="width: 192px;">
          <el-option v-for="dict in warehouse_in_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['stock:in:add']">新增</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['stock:in:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['stock:in:remove']">删除</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['stock:in:export']">导出</el-button>
      </el-col> -->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="inList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="入库编号" align="center" min-width="188" prop="warehouseInNo" />
      <el-table-column label="来源单号" align="center" min-width="188" prop="sourceNo" />
      <el-table-column label="入库类型" align="center" prop="goodsType">
        <template #default="scope">
          <dict-tag :options="warehouse_in_store_type" :value="scope.row.goodsType" />
        </template>
      </el-table-column>
      <el-table-column label="入库状态" align="center" prop="warehouseInStatus">
        <template #default="scope">
          <dict-tag :options="warehouse_in_type" :value="scope.row.warehouseInStatus" />
        </template>
      </el-table-column>
      <el-table-column label="入库时间" align="center" prop="warehouseInDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.warehouseInDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="入库人" align="center" prop="userName" />
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
        <template #default="scope">
          <!-- <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['stock:in:edit']">修改</el-button> -->
          <!-- <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['stock:in:remove']">删除</el-button> -->
          <el-button link type="primary" icon="View" @click="look(scope.row)" v-hasPermi="['stock:in:list']"
            :disabled="scope.row.warehouseInStatus == 0">查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改入库对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="inRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="入库编号" prop="warehouseInNo">
          <el-input v-model="form.warehouseInNo" placeholder="请输入入库编号" />
        </el-form-item>
        <el-form-item label="来源单号" prop="sourceNo">
          <el-input v-model="form.sourceNo" placeholder="请输入来源单号" />
        </el-form-item>
        <el-form-item label="入库类型" prop="goodsType">
          <el-select v-model="form.goodsType" placeholder="请选择入库类型">
            <el-option v-for="dict in warehouse_in_store_type" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="入库状态" prop="warehouseInStatus">
          <el-select v-model="form.warehouseInStatus" placeholder="请选择入库状态">
            <el-option v-for="dict in warehouse_in_type" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="入库时间" prop="warehouseInDate">
          <el-date-picker clearable v-model="form.warehouseInDate" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择入库时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="In">
import { listIn, getIn, delIn, addIn, updateIn } from "@/api/stock/in"
import { useRouter } from "vue-router"
import useInStockStore from "../../../store/modules/inStock"

const { proxy } = getCurrentInstance()
const { warehouse_in_store_type, warehouse_in_type } = proxy.useDict('warehouse_in_store_type', 'warehouse_in_type')

const router = useRouter()

const inList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const inStockStore = useInStockStore()

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    warehouseInNo: null,
    sourceNo: null,
    goodsType: null,
    warehouseInStatus: null,
    warehouseInDate: null,
    userId: null
  },
  rules: {
    warehouseInNo: [
      { required: true, message: "入库编号不能为空", trigger: "blur" }
    ],
    sourceNo: [
      { required: true, message: "来源(关联销售订单或采购订单)不能为空", trigger: "blur" }
    ],
    goodsType: [
      { required: true, message: "入库类型(生产入库/采购入库/退料入库)(字典)不能为空", trigger: "change" }
    ],
    warehouseInStatus: [
      { required: true, message: "入库状态(待质检,待入库,已入库)不能为空", trigger: "change" }
    ],
    warehouseInDate: [
      { required: true, message: "入库时间不能为空", trigger: "blur" }
    ],
    userId: [
      { required: true, message: "入库人id不能为空", trigger: "blur" }
    ]
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询入库列表 */
function getList() {
  loading.value = true
  listIn(queryParams.value).then(response => {
    inList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    warehouseInNo: null,
    sourceNo: null,
    goodsType: null,
    warehouseInStatus: null,
    warehouseInDate: null,
    userId: null
  }
  proxy.resetForm("inRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加入库"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getIn(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改入库"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["inRef"].validate(valid => {
    if(valid) {
      if(form.value.id != null) {
        updateIn(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addIn(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除入库编号为"' + _ids + '"的数据项？').then(function() {
    return delIn(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => { })
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('stock/in/export', {
    ...queryParams.value
  }, `in_${new Date().getTime()}.xlsx`)
}

//查看详情
function look(row) {
  //将入库单id和当前单的质检状态放到pinia存储并跳转页面
  inStockStore.clearStore()
  inStockStore.goodsType = row.goodsType
  inStockStore.warehouseInId = row.id
  inStockStore.warehouseInStatus = row.warehouseInStatus
  router.push('/stock/inInfo')
}

getList()
</script>

<style scoped>
.el-input {
  width: 192px;
}

.el-dialog .el-input {
  width: 3988px;
}
</style>